package com.jiyici.view;

import jiyici.ParamType;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jiyici.R;
import com.jiyici.model.ItemParam;
import com.jiyici.view.DialogUtil.Notify;
import com.jiyici.view.ItemEditView.PropertyAdapter;

public class ItemPropertyView extends LinearLayout {

    public ItemPropertyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        inflater();
    }

    public ItemPropertyView(Context context) {
        super(context);
        inflater();
    }

    public void setParam(ItemParam param, PropertyAdapter propertyAdapter) {
        mItemParam = param;
        mPropertyAdapter = propertyAdapter;
        mTypeIcon.setImageResource(ParamType.getParamType(param.paramType).typeIcon);
        mEditParamName.setText(param.itemParamName);
    }

    private void inflater() {
        LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.view_item_param, this, true);
        mTypeIcon = (ImageView) view.findViewById(R.id.property_type_icon);
        mEditParamName = (TextView) view.findViewById(R.id.param_name);
        mBtnDelete = view.findViewById(R.id.param_delete);

        mBtnDelete.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mPropertyAdapter.deleteParam(mItemParam);
                if (mItemParam.getId() > 0) {
                    DialogUtil.showNofity(getContext(), Notify.NOTIFY_WHEN_DELETE_PROPERTY);
                }
            }
        });
    }

    private PropertyAdapter mPropertyAdapter;
    private ItemParam mItemParam;
    private ImageView mTypeIcon;
    private TextView mEditParamName;
    private View mBtnDelete;
}
